package cn.ld.gift.service.impl;

import cn.ld.exception.ass.LdAssert;
import cn.ld.exception.code.ErrorCode;
import cn.ld.gift.client.AuthClient;
import cn.ld.gift.domain.Employee;
import cn.ld.gift.domain.LoginUser;
import cn.ld.gift.domain.Tenant;
import cn.ld.gift.mapper.EmployeeMapper;
import cn.ld.gift.mapper.TenantMapper;
import cn.ld.gift.query.TenantQuery;
import cn.ld.gift.service.ITenantService;
import cn.ld.util.AjaxResult;
import cn.ld.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ld
 * @since 2023-02-24
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthClient authClient;


    @Override
    public PageList loadList(TenantQuery query) {

        Page<Tenant> page = new Page<>(query.getPage(),query.getRows());

        List<Tenant> datas =  tenantMapper.loadPageList(page,query);


        return new PageList(page.getTotal(), datas);
    }

    @Override
    public AjaxResult settled(Tenant tenant) {
        //非空参数校验
        String companyName = tenant.getCompanyName();
        LdAssert.isNotBlank(companyName, "不输参数，入驻个锤子");
        //其它的一样，调用断言不为空的方法判断就行

        //判断租户是否存在
        List<Tenant> tenantList = tenantMapper.selectList(new EntityWrapper<Tenant>()
                .eq("company_name", companyName));
        //断言为空，不为空则报错
       // LdAssert.isEmpty(tenantList,ErrorCode.ERROR_CODE_TENANT_EXIST);

        //判断管理员是否存在，存在就直接登录
        List<Employee> employees = employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("username", tenant.getAdmin().getUsername()));
        //断言管理员不存在，存在就抛异常
      //  LdAssert.isEmpty(employees,ErrorCode.ERROR_CODE_TENANT_ADMIN_EXIST);

        //模拟自动审核
        boolean auditResult = true;
        Employee admin = tenant.getAdmin();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setPassword(admin.getPassword());
        loginUser.setTel(admin.getTel());
        loginUser.setEmail(admin.getEmail());
        loginUser.setType(0);
        //保存loginuser，为了获得其主键
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        //保存admin
        Long loginId =  loginUser.getId(); ;
        admin.setLoginId(loginId);
        admin.setInputTime(new Date());
        admin.setRealName(admin.getUsername());
        admin.setState(0);
        admin.setType(1);
        employeeMapper.insert(admin);

        //保存租户的信息
        tenant.setAdminId(admin.getId());
        tenant.setRegisterTime(new Date());
        tenant.setState(auditResult?1:0);
        tenantMapper.insert(tenant);

        //保存套餐
        Long tenantId = tenant.getId();
        Long mealId = tenant.getMealId();
        tenantMapper.saveMeals(tenantId,mealId);

        //模拟审核和发信息
        if (!auditResult){
            return AjaxResult.me().setSuccess(false)
                    .setMessage("审核失败!").setResultObj(tenantId);
        }else{
            return AjaxResult.me();
        }
    }
}
